<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="50%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      :disabled="mode == 'show'"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="form.title"
          clearable
          placeholder="请输入标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="地点" prop="address">
        <el-input
          v-model="form.address"
          clearable
          placeholder="请输入地点"
        ></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content" style="width: 100%">
        <el-input
          v-model="form.content"
          clearable
          placeholder="请输入内容"
          type="textarea"
          rows="3"
        ></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="form.startTime"
          type="datetime"
          placeholder="请选择计划回款日期"
          value-format="YYYY-MM-DD HH:mm:ss"
          :disabled-date="disabledDate"
          :default-time="new Date()"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="form.endTime"
          type="datetime"
          placeholder="请选择计划回款日期"
          value-format="YYYY-MM-DD HH:mm:ss"
          :disabled-date="disabledDate"
          :default-time="new Date()"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="参与人" prop="ownerAdminList">
        <el-select
          v-model="form.ownerAdminList"
          placeholder="请选择参与人"
          style="width: 100%"
          clearable
          multiple
        >
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否提醒" prop="eventType" style="width: 100%">
        <el-switch
          v-model="form.eventType"
          :inactive-value="0"
          :active-value="1"
        />
      </el-form-item>
      <el-form-item
        label="提醒时间"
        prop="remindType"
        v-if="form.eventType == 1"
      >
        <el-select
          v-model="form.remindType"
          placeholder="请选择提醒时间"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="item in remindTypeList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
import { queryTypeByDic } from '@/utils/getData'
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      selectCustomerLoading: false,
      //表单数据
      form: {
        createAdminId: '',
        title: '',
        content: '',
        address: '',
        startTime: '',
        endTime: '',
        ownerAdminList: [],
        ownerAdminIds: '',//参与人用户id
        eventType: 0,//提醒状态：0不提醒 1提醒
        remindType: 0,//提醒时间：0准时提醒1：5分钟前2：15分钟前3：30分钟前4：一个小时前5：两个小时前6：一天前7：两天前8：一周前
      },
      // userList: [],
      remindTypeList: [],//提醒时间类型
      //验证规则
      rules: {
        title: [
          { required: true, message: '请输入标题', }
        ],
        content: [
          { required: true, message: '请输入内容', }
        ],
        address: [
          { required: true, message: '请输入地点', }
        ],
        startTime: [
          {
            type: 'date',
            required: true,
            message: '请选择开始时间',
            trigger: 'change',
          },
        ],
        endTime: [
          {
            type: 'date',
            required: true,
            message: '请选择结束时间',
            trigger: 'change',
          },
        ],
      }
    }
  },
  props: {
    userList: {
      type: Array,
      required: true,
      default: () => { return [] }
    }
  },
  mounted () {
    this.getType()
  },

  methods: {
    // 获取提醒时间类型
    async getType () {
      let type = await queryTypeByDic('remindType')
      this.remindTypeList = type
    },
    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      let userInfo = this.$TOOL.data.get('USER_INFO')
      this.form.createAdminId = userInfo.userId
      return this
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let data = { ...this.form }
          data.ownerAdminIds = this.form.ownerAdminList.join(',')
          delete data.ownerAdminList
          console.log(data)
          let res = null
          if (this.mode == 'add') {
            res = await this.$API.schedule.addSchedule.post(data)
          } else if (this.mode == 'edit') {
            delete data.createTime
            delete data.updateTime
            res = await this.$API.schedule.updateSchedule.post(data)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          }
        }
      })
    },

    //表单注入数据
    setData (data) {
      this.form = data
      let list = data.ownerAdminIds.split(',')
      let ids = []
      this.userList.forEach(item => {
        list.map(l_item => {
          if (item.username == l_item) {
            ids.push(item.id)
          }
        })
      })

      this.form.ownerAdminList = ids
    },
    // 设置就日期选择范围  今天之后
    disabledDate (time) {
      return time.getTime() < Date.now() - 8.64e7//如果没有后面的-8.64e7就是不可以选择
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
